<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */

namespace app\command;

use app\enum\ConfigEnum;
use app\enum\OceanEnum;
use app\service\batch\OceanBatchService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class OeMongoVideoStatusWork extends BaseWork
{
    public const REDIS_TASK_KEY = ConfigEnum::CACHE_CONFIG[OceanEnum::BATCH_CONFIG_KEY]['mongo_video_all_status'];

    protected function configure()
    {
        // 指令配置
        $this->setName('oe_mongo_video_status_work');
        // 设置参数
        $this->setDescription('mongo修改视频删除状态列表');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $this->getOutputMessage('巨量引擎-修改视频删除状态');

        $redis = Cache::store('redis')->handler();
        $st = time() + 5 * 60;
        while (time() < $st) {
            try {
                $versionInfo = $redis->lPop(self::REDIS_TASK_KEY);
                if (! $versionInfo) {
                    usleep(500000);
                    continue;
                }
                $arr = explode('|', $versionInfo);
                $accountId = intval($arr[0]);
                $version = intval($arr[1]);
                $this->getOutputMessage('巨量引擎-修改视频删除状态,账户[' . $accountId . ']开始');
                OceanBatchService::batchMongoVideoStatus($accountId, $version);
                $this->getOutputMessage('巨量引擎-修改视频删除状态,账户[' . $accountId . ']结束');
            } catch (\Exception $e) {
                $this->getOutputMessage('巨量引擎-修改视频删除状态失败');
            }
        }
        $this->getOutputMessage('巨量引擎-修改视频删除状态结束');
    }
}
